

class unionUserService {


    constructor() {

        this.unionList = {}; //联盟列表数据
        this.unionUserList = {}; //联盟用户数据
    }

    static getInstance() {
        if (!unionUserService.instance) {
            unionUserService.instance = new unionUserService();
        }
        return unionUserService.instance;
    }

    //保存联盟的基础信息
    addUnionData(unionID, data) {

        this.unionList[unionID] = data;
    }

    //获取联盟基础信息
    getUnionByID(unionID) {
        return this.unionList[unionID];
    }

    //获取所有联盟信息
    getAllUnionData() {
        return this.unionList;
    }

    //联盟用户数据
    addUnionUser(unionID, userData) {

        this.unionUserList[unionID] = userData;

    }

    //获取所有的下级玩家ID
    getLowerUser(unionID, userID) {

        let lowUserList = [];
        //联盟数据
        let unionData = this.unionUserList[unionID];
        //查找到当前用户
        let userData = unionData.find(item => item.uid == userID);
        if (userData) {
            //查找下级
            let isHaveNext = true;
            let superList = [userData.uid];
            if (userData.partner) {
                while (isHaveNext) {
                    let nextUser = unionData.filter(item => superList.includes(item.spreaderID));
                    if (nextUser.length > 0) {
                        superList = [];
                        nextUser.map(item => {
                            superList.push(item.uid);
                            lowUserList.push(item.uid);
                        })
                    } else {
                        isHaveNext = false;
                    }
                }
            }
        }
        return lowUserList;
    }

    //获取所有上级信息
    getSpreaderUser(unionID, userID) {

        let spreaderList = [];
        //联盟数据
        let unionData = this.unionUserList[unionID];
        //查找到当前用户
        let userData = unionData.find(item => item.uid == userID);
        if (userData) {
            //查找上级
            let isHaveNext = true;
            let spreaderID = userData.spreaderID;
            while (isHaveNext && spreaderID) {

                spreaderList.push(spreaderID);
                let nextUser = unionData.find(item => item.uid == spreaderID);
                if (nextUser) {
                    // spreaderList.push(nextUser.spreaderID);
                    if (nextUser.spreaderID) {
                        spreaderID = nextUser.spreaderID;
                    } else {
                        isHaveNext = false;
                    }
                } else {
                    isHaveNext = false;
                }
            }
        }
        return spreaderList;
    }

}

module.exports = unionUserService;
